package com.changgou.seckill.controller;

import com.changgou.common.pojo.DateUtil;
import com.changgou.common.pojo.Result;
import com.changgou.common.pojo.SeckillStatus;
import com.changgou.common.pojo.StatusCode;
import com.changgou.seckill.pojo.SeckillGoods;
import com.changgou.seckill.service.SeckillGoodsServcie;
import com.changgou.seckill.service.SeckillOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @author tu zy
 * @version 1.0
 * @date 2021/7/13 22:06
 */
@RestController
@RequestMapping("/seckillGoods")
@CrossOrigin
public class SeckillGoodsController {

    @Autowired
    private SeckillGoodsServcie seckillGoodsServcie;

    @Autowired
    private SeckillOrderService seckillOrderService;

    /**
     * 根据时间和ID查询秒杀商品详情
     */
    @GetMapping(value = "/one")
    public Result<SeckillGoods> one(@RequestParam String time,@RequestParam Long id){
        SeckillGoods seckillGoods = seckillGoodsServcie.one(time, id);
        return new Result<SeckillGoods>(true,StatusCode.OK,"查询秒杀商品成功",seckillGoods);
    }


    /**
     * 查询秒杀商品时间菜单
     */
    @GetMapping(value = "/menus")
    public Result<List<Date>> menus(){

        List<Date> dateMenus = DateUtil.getDateMenus();

        return new Result<List<Date>>(true,StatusCode.OK,"查询秒杀时间菜单成功！",dateMenus);

    }



    /**
     * 根据时间区间查询秒杀商品频道列表数据
     */
    @GetMapping(value = "/list")
    public Result<List<SeckillGoods>> list(String time){
        //调用service查询
        List<SeckillGoods> seckillGoods = seckillGoodsServcie.list(time);

        System.out.println("");

        return new Result<List<SeckillGoods>>(true, StatusCode.OK,"秒杀商品列表查询",seckillGoods);

    }


    /**
     * 添加秒杀订单
     * @param time
     * @parm id
     */
    @RequestMapping(value = "/add")
    public Result add(@RequestParam String time,@RequestParam Long id){
        String username = "tuzy";

        seckillOrderService.add(time,id,username);

        return new Result(true,StatusCode.OK,"下单成功");
    }

    /**
     * 抢单状态查询
     */
    @GetMapping(value = "query")
    public Result queryStatus(){
        String username = "szitheima";
        SeckillStatus seckillStatus = seckillOrderService.queryStatus(username);

        //查询成功
        if (seckillStatus!=null){

            return new Result<>(true,StatusCode.OK,"查询状态成功",seckillStatus);

        }
        return new Result(false,StatusCode.NOTFOUNDERROR,"没有抢购信息");
    }




}
